﻿MPT.addAction("upper_my_info", function(_elm) {

    var comment_jQ = $('.e_comment', _elm);
    var nickname_jQ = $('.e_nickname', _elm);
    var area_jQ = $('.e_area', _elm);
    var level_jQ = $('.e_level', _elm);
    var mysign_jQ = $('.e_mysign', _elm);
    var avatar_jQ = $('.e_avatar', _elm);

    var sns_score_jQ = $('.e_sns_score', _elm);
    var game_score_jQ = $('.e_game_score', _elm);
    var contribute_jQ = $('.e_contribute', _elm);
    var game_times_jQ = $('.e_game_times', _elm);

    var input_jQ = $('.e_input', _elm);
    var remain_jQ = $('.e_remain', _elm);
    var publish_jQ = $('.e_publish', _elm);
    var submit_jQ = $('.e_submit', _elm);
    var cancel_jQ = $('.e_cancel', _elm);


    //创建微博发送框
    (new Publish).create(comment_jQ[0]);

    input_jQ.mousedown(function(e) {
        //评论框显示出来之前，禁止隐藏，防止用户过快点击
        $(document).not(comment_jQ[0]).unbind('mousedown');

        input_jQ.animate({ height: '79px' }, "fast", function() {

            input_jQ.focus();
            //点其他地方，隐藏评论框
            $(document).not(comment_jQ[0]).bind('mousedown', function(e) {
                hideComment();
            });
            //取消评论框的事件冒泡，防止点评论框也会隐藏
            comment_jQ.mousedown(function(e) {
                e.stopPropagation();
            });
        });
        remain_jQ.show();
        publish_jQ.show();

        e.stopPropagation();
    });

    //点击“发言”
    submit_jQ.click(function() {
        sendBlogMessage('', true);
    });

    //点击取消按钮
    cancel_jQ.click(function() {
        hideComment();
    });

    //隐藏评论框
    var hideComment = function() {
        $('.e_emotion', _elm).hide();
        $('.e_friend', _elm).hide();
        $('.e_topic', _elm).hide();
        if (input_jQ.val().length > 150) {

        } else {
            input_jQ.animate({ height: '20px' }, "fast", function() {
                remain_jQ.hide();
                publish_jQ.hide();
                $(document).not(comment_jQ[0]).unbind('mousedown');
            });
        }
    };

    // 发表微博
    var sendBlogMessage = function(_frontWord, _isVal) {

        var content = _frontWord + $(".e_input").val();
        //var setSiganture = $("#setSiganture").checked;

        var data = { 'content': content, "setSiganture": false, "val": _isVal };

        Backend.publish(data, function(_resp) {
            if (Backend.dataValidate(_resp)) {
                var msg = _resp.data;

                if (msg.publishMark == '1') {
                    Tip.show('请勿广播重复内容');
                } else if (msg.publishMark == '2') {
                    Tip.show('频率太快 稍后再发');
                } else {
                    $("#content").val("");
                    hideComment();
                    $('.e_myActive').click();
                }
            } else {
                Tip.show('发言失败');
            }
        }, function() {
            Tip.show('发言失败');
        });
    };


    Backend.findUserInfo(null, function(_resp) {
        if (Backend.resultValidate(_resp)) {
            var result = _resp.data;
            var userID = result.uid;
            var nickname = result.nickname;
            var mysign = Util.truncName(result.signature, 30);
            var grade = Util.transformGradeToIcon(result.grade);
            var avatarPath = Util.avatarPath(userID);
            var area = result.area;
            var game_score = result.activityScore;
            var sns_score = result.spaceScore;
            var contribute = result.contributeValue;
            var game_times = 8 - parseInt(result.todayPlayCount) < 0 ? 0 : 8 - parseInt(result.todayPlayCount);

            sns_score_jQ.html(sns_score);
            game_score_jQ.html(game_score);
            contribute_jQ.html(contribute);
            game_times_jQ.html(game_times);

            nickname_jQ.html(nickname);
            avatar_jQ.attr('src', avatarPath);
            mysign_jQ.html(mysign == null || mysign == "" ? "你还没有签名，快去添加你的个性签名吧" : mysign); //todo:无签名
            level_jQ.html(grade);

            if (area && area != '') {
                SiteUtil.getServerZone(function(_areaList) {
                    area_jQ.html("【" + _areaList[area] + "】");
                });
            }
        } else {
            Tip.show("用户信息加载错误");
        }
    }, function() {
        Tip.show("用户信息加载错误");
    });

});